/*
 * Copyright 2011 Philippe Blanc.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package me.l1k3.app.ui.demo.client;

import me.l1k3.parameter.client.Callback;
import me.l1k3.parameter.client.Parameter;
import me.l1k3.ui.client.*;
import me.l1k3.ui.client.event.ShowAndHidePanelEvent;
import me.l1k3.ui.client.event.ShowAndHidePanelEventHandler;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class Demo implements EntryPoint {
    private ShowAndHidePanelEx slideA;
    private ShowAndHidePanelEx slideB;
    private ShowAndHidePanelEx slideC;
    
    @Override
    @SuppressWarnings("all")
    public void onModuleLoad() {
        Parameter.load("ui", new Callback() {
            @Override
            public void run(JavaScriptObject js) {
                load(js);
            }
        });
    }
    
    private void load(JavaScriptObject js) {
        Parameters parameters = js.cast();
        
        slideA = new ShowAndHidePanelEx(true);
        slideA.addToContent(new HTML("<h3>Slide A</h3>"));
        slideA.addToContent(new HTML("Lorem ipsum dolor sit amet, consectetuer adipiscing elit."));
        slideA.addToContent(new HTML("Etiam sit amet elit vitae arcu interdum ullamcorper."));
        
        Anchor blockA = new Anchor("Submit and close");
        
        blockA.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                event.stopPropagation();
                blockA();
            }
        });
        
        slideA.addToContent(blockA);
        slideA.addCloseButton("close");
        
        //- - -
        
        slideB = new ShowAndHidePanelEx(false);
        slideB.addToContent(new HTML("<h3>Slide B</h3>"));
        slideB.addToContent(new HTML("Lorem ipsum dolor sit amet, consectetuer adipiscing elit."));
        slideB.addToContent(new HTML("Etiam sit amet elit vitae arcu interdum ullamcorper."));
        
        Anchor blockB = new Anchor("Submit");
        
        blockB.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                event.stopPropagation();
                blockB();
            }
        });
        
        slideB.addToContent(blockB);
        slideB.addCloseButton("close");
        
        //- - -
        
        slideC = new ShowAndHidePanelEx(false);
        slideC.addToContent(new HTML("<h3>Slide C</h3>"));
        slideC.addToContent(new HTML("Lorem ipsum dolor sit amet, consectetuer adipiscing elit."));
        slideC.addToContent(new HTML("Etiam sit amet elit vitae arcu interdum ullamcorper."));
        
        Anchor blockC = new Anchor("Submit");
        
        blockC.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                event.stopPropagation();
                blockC();
            }
        });
        
        slideC.addToContent(blockC);
        slideC.addCloseButton("close");

        //- - -

        RootPanel root = RootPanel.get();
        
        if(parameters.get("auto", true)) {
            ShowAndHidePanelGroup group = new ShowAndHidePanelGroup();
            slideA.setGroup(group);
            slideB.setGroup(group);
            slideC.setGroup(group);
        }
        
        Widget buttonA;
        Widget buttonB;
        Widget buttonC;
        
        if(parameters.get("button", true)) {
            buttonA = slideA.createButton("Show Slide A", "Lorem ipsum dolor sit amet");
            buttonB = slideB.createButton("Show Slide B", "Lorem ipsum dolor sit amet");
            buttonC = slideC.createButton("Show Slide C", "Lorem ipsum dolor sit amet");
        }
        else {
            buttonA = slideA.createAnchor("Show Slide A", "Lorem ipsum dolor sit amet");
            buttonB = slideB.createAnchor("Show Slide B", "Lorem ipsum dolor sit amet");
            buttonC = slideC.createAnchor("Show Slide C", "Lorem ipsum dolor sit amet");
        }
        
        //listener
        slideA.addShowAndHidePanelEventHandler(new ShowAndHidePanelEventHandler() {
            @Override
            public void show(ShowAndHidePanelEvent showAndHidePanelEvent) {
                System.out.println("EventBus: Show SlideA");
            }
            
            @Override
            public void hide(ShowAndHidePanelEvent showAndHidePanelEvent) {
                System.out.println("EventBus: Hide SlideA");
            }
        });
        
        int demo = parameters.get("demo", 1);
        
        if(demo==1) {
            FlowPanel slides = new FlowPanel();
            slides.getElement().setClassName("slides");
            slides.getElement().addClassName("accordion");
            
            slides.add(buttonA);
            slides.add(slideA);
            slides.add(buttonB);
            slides.add(slideB);
            slides.add(buttonC);
            slides.add(slideC);
            
            root.add(slides);
        }
        else
        if(demo==2) {
            FlowPanel slides = new FlowPanel();
            slides.getElement().setClassName("slides");
            slides.getElement().addClassName("flow");
            
            slides.add(buttonA);
            slides.add(slideA);
            slides.add(buttonB);
            slides.add(slideB);
            slides.add(buttonC);
            slides.add(slideC);
            
            root.add(slides);
        }
        else
        if(demo==3) {
            FlowPanel tabs = new FlowPanel();
            tabs.getElement().setClassName("tabs");
            
            tabs.add(buttonA);
            tabs.add(buttonB);
            tabs.add(buttonC);
            
            tabs.add(new HTML("<div class=\"clear\"></div>"));
            
            root.add(tabs);
            
            FlowPanel slides = new FlowPanel();
            slides.getElement().setClassName("slides");
            slides.getElement().addClassName("slide-panels");
            
            slides.add(slideA);
            slides.add(slideB);
            slides.add(slideC);
            
            root.add(slides);
        }
        else
        if(demo==4) {
            FlowPanel slides = new FlowPanel();
            slides.getElement().setClassName("slides-top");
            slides.getElement().addClassName("slide-panels");
            
            slides.add(slideA);
            slides.add(slideB);
            slides.add(slideC);
            
            root.add(slides);
            
            FlowPanel tabs = new FlowPanel();
            tabs.getElement().setClassName("tabs-bottom");
            
            tabs.add(buttonA);
            tabs.add(buttonB);
            tabs.add(buttonC);
            
            tabs.add(new HTML("<div class=\"clear\"></div>"));
            
            root.add(tabs);
        }
    }
    
    /**
     * testing callback... lot of them
     */
    protected void blockA() {
        slideA.block(new Command() {
            @Override
            public void execute() {
                System.out.println("Callback: SlideA is blocked");
            }
        });
        
        Timer timer = new Timer() {
            @Override
            public void run() { 
                
            slideA.release(new Command() {
                @Override
                public void execute() {
                    System.out.println("Callback: SlideA is released");
                }
            }); 
            
            slideA.hide(new Command() {     
                    @Override
                    public void execute() {
                        System.out.println("Callback: SlideA is closed");
                    }
                }
            );}
        };
        
        timer.schedule(2000);
    }
    
    protected void blockB() {
        slideB.block();
        
        Timer timer = new Timer() {
            @Override
            public void run() { slideB.release(); }
        };
        
        timer.schedule(2000);
    }
    
    protected void blockC() {
        slideC.block();
        
        Timer timer = new Timer() {
            @Override
            public void run() { slideC.release(); }
        };
        
        timer.schedule(2000);
    }
    
    protected static class Parameters extends JavaScriptObject {
        
        protected Parameters() {
            
        }
        
        public final native String get(String key, String defaultValue) /*-{
            return this[key] || defaultValue;
        }-*/;
        
        public final native boolean get(String key, boolean defaultValue) /*-{
            return this[key] || defaultValue;
        }-*/;
        
        public final native int get(String key, int defaultValue) /*-{
            return this[key] || defaultValue;
        }-*/;
        
        public final native double get(String key, double defaultValue) /*-{
            return this[key] || defaultValue;
        }-*/;
    }
}
